package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsMapper;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.TreeNode;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Map<String, Object> getListInventory(Integer page, Integer rows, String codeOrName, String goodsTypeId) {
        PageHelper.startPage(page, rows);
        List<Goods> list = goodsMapper.getList(codeOrName, goodsTypeId);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", pageInfo.getList());
        return map;
    }

    @Override
    public TreeNode loadGoodsType() {
        TreeNode treeNode = new TreeNode();
        List<GoodsType> typeList1 = goodsMapper.getTypeList(-1);
        GoodsType type1 = typeList1.get(0);
        if (null != type1) {
            treeNode.setId(type1.getGoodsTypeId());
            treeNode.setText(type1.getGoodsTypeName());
            treeNode.setState(type1.getGoodsTypeState() == 0 ? "open" : "closed");
            treeNode.setIconCls("goods_type");
            HashMap<String, Integer> map = new HashMap<>();
            map.put("state", type1.getGoodsTypeState());
            treeNode.setAttributes(map);

            List<GoodsType> type2List = goodsMapper.getTypeList(type1.getGoodsTypeId());
            tempMethod(treeNode,type2List);
            for (TreeNode child2 : treeNode.getChildren()) {
                List<GoodsType> type3List = goodsMapper.getTypeList(child2.getId());
                if (null==type3List||type3List.size()<1){
                    child2.setState("0");
                }
                tempMethod(child2,type3List);
            }
        }
        return treeNode;
    }

    @Override
    public List<Unit> getUnitList() {
        return goodsMapper.getUnitList();
    }

    @Override
    public Map<String, Object> getList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        PageHelper.startPage(page, rows);
        List<Goods> list = goodsMapper.getList(goodsName, String.valueOf(goodsTypeId));
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", pageInfo.getList());
        return map;
    }

    @Override
    public void saveType(String goodsTypeName, Integer pId) {
        goodsMapper.saveType(goodsTypeName,pId);
    }

    @Override
    public void delType(Integer goodsTypeId) {
        goodsMapper.delType(goodsTypeId);
    }

    @Override
    public void saveGoods(Goods goods) {
        Integer goodsId = goods.getGoodsId();
        if (null!=goodsId&&!"".equals(goodsId)){

            goodsMapper.updateGoods(goods);

        }else {
            goodsMapper.insertGoods(goods);
        }

    }

    @Override
    public Goods getGoods(Integer goodsId) {
        return goodsMapper.getGoods(goodsId);
    }

    @Override
    public void delGoods(Integer goodsId) {
        goodsMapper.delGoods(goodsId);
    }



    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page,rows);
        List<Goods> list = goodsMapper.getNoInventoryQuantity(nameOrCode);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page,rows);
        List<Goods> list = goodsMapper.getHasInventoryQuantity(nameOrCode);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }

    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsMapper.updateStock(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public void deleteStock(Integer goodsId) {
        goodsMapper.delStock(goodsId);
    }

    @Override
    public Map<String, String> getCode() {
        HashMap<String, String> map = new HashMap<>();
        ArrayList<String> goodsCode = goodsMapper.getGoodsCode();
        String mCode = generateCode(goodsCode.get(goodsCode.size() - 1));
        map.put("info",mCode);
        return map;
    }

    /**
     * 循环赋值方法
     *  list->treeNode.setChildren()
     * @param treeNode
     * @param list
     * @return
     */
    private TreeNode tempMethod(TreeNode treeNode, List<GoodsType> list) {
        if (null != list && list.size() > 0) {
            ArrayList<TreeNode> children = new ArrayList<>();
            for (GoodsType goodsType : list) {
                TreeNode node = new TreeNode();
                node.setId(goodsType.getGoodsTypeId());
                node.setText(goodsType.getGoodsTypeName());
                node.setState(goodsType.getGoodsTypeState() == 0 ? "open" : "closed");
                node.setIconCls("goods-type");
                HashMap<String, Integer> map = new HashMap<>();
                map.put("state", goodsType.getGoodsTypeState());
                node.setAttributes(map);
//                List<GoodsType> type3List = goodsMapper.getType3(goodsType.getGoodsTypeId());
                children.add(node);
//                    node.setChildren(null);
            }
            treeNode.setChildren(children);
        }
        return treeNode;
    }
//    //所有分类
//    public List<GoodsTypeResult> loadGoodsType() {
//        //查出所有分类对象GoodsType集合
//        List<GoodsType> goodsTypeList = goodsTypeDao.findAllType();
//
//        Map<Integer, List<GoodsType>> map = goodsTypeList.stream()
//                .collect(Collectors.groupingBy(GoodsType::getPId));
//        List<GoodsType> oneLevelList = goodsTypeList.stream().filter(goodsType -> goodsType.getPId() == -1).collect(Collectors.toList());
//        List<GoodsTypeResult> list = new ArrayList<>();
//
//        oneLevelList.forEach(goodsType -> {
//            GoodsTypeResult goodsTypeResult = this.levelType(goodsType,map);
//            list.add(goodsTypeResult);
//        });
//
//        return list;
//    }
//
//
//    //分级封装类型
//    private GoodsTypeResult levelType(GoodsType goodsType, Map<Integer, List<GoodsType>> map) {
//        GoodsTypeResult goodsTypeResult = new GoodsTypeResult();
//        goodsTypeResult.setId(goodsType.getGoodsTypeId());
//        goodsTypeResult.setText(goodsType.getGoodsTypeName());
//        goodsTypeResult.setIconCls("goods-type");
//        goodsTypeResult.setState(goodsType.getGoodsTypeState()==0?"open":"closed");
//        HashMap<Object, Object> hashMap = new HashMap<>();
//
//        hashMap.put("state", goodsType.getGoodsTypeState());
//        goodsTypeResult.setAttributes(hashMap);
//
//        if (map.containsKey(goodsType.getGoodsTypeId())){
//            List<GoodsType> goodsTypeList = map.get(goodsType.getGoodsTypeId());
//            List<GoodsTypeResult> childrenList = goodsTypeList.stream().map(subGoodsType -> {
//                GoodsTypeResult subGoodsTypeResult = this.levelType(subGoodsType, map);
//                return subGoodsTypeResult;
//            }).collect(Collectors.toList());
//            goodsTypeResult.setChildren(childrenList);
//        }
//        return goodsTypeResult;
//    }
private String generateCode(String maxCode){
    Integer mCode = Integer.parseInt(maxCode);
    if (++mCode<1000){
        if (mCode<=9&&mCode>0)return "000"+mCode;
        if (mCode>=10&&mCode<=99)return "00"+mCode;
        if (mCode>=100&&mCode<=999)return "0"+mCode;
    }
    return String.valueOf(mCode);
}
}
